package com.peng.service;

import com.peng.mapper.SignMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Service
public class SignServiceImp implements SignService {
    @Autowired
    private SignMapper signMapper;

    @Override
    public boolean addSignInByUserId(int userId) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        //判断昨天是否签到
        if (signMapper.selectyesterdayIsSign(userId, yesterdayDate) > 0) {
            signMapper.updateContinuousSignByUserId(userId, 1);
        } else {
            signMapper.updateContinuousSignByUserId(userId, null);
        }
        //判断用户是否达到连续签到10次，获取相应的托福币
        selectSignNumber(userId);
        signMapper.addSign(userId);
        return true;
    }

    @Override
    public boolean selectSignNumber(int userId) {
        if (signMapper.selectSignNumber(userId) >= 10) {
            //System.out.println("达到10天");
            signMapper.updateAccountByUserId(userId);
        } else {
            //System.out.println("未达到10天");
        }
        return true;
    }
}
